package org.zoomdev.zoom.web.view.impl;

import jakarta.servlet.http.HttpServletResponse;
import org.zoomdev.zoom.web.utils.ResponseUtils;
import org.zoomdev.zoom.web.view.View;

/**
 * 渲染一个json
 *
 * @author jzoom
 */
public final class JsonView implements View {

    private final boolean pretty;
    private final Object data;
    private final int status;

    /**
     * 需要转化的object
     *
     * @param data
     */
    public JsonView(Object data) {
        this(200, data, false);
    }

    /**
     * 自定义http status用这个构造函数
     *
     * @param status
     * @param data
     */
    public JsonView(int status, Object data, boolean pretty) {
        this.status = status;
        this.data = data;
        this.pretty = pretty;
    }

    public JsonView(Object data, boolean pretty) {
        this(200, data, pretty);
    }

    @Override
    public void render(HttpServletResponse response) throws Exception {
        response.setStatus(status);
        ResponseUtils.writeJson(response, data, pretty);
    }

}
